/*
 * IocService.cpp
 *
 *  Created on: 2021?11?20?
 *      Author: solq
 */
#include "../common.h"

extern __gnu_cxx ::hash_map<int, IBean*> _beans(_end);

IBean* IocService::getBean(BeanKey key) {
	return _beans[key];
}

void IocService::registerBean(BeanKey key, IBean *bean) {
	_beans[key] = bean;
}

void IocService::init() {
	app_injection_bean(MODEL_REPOSITORY,RepositoryService)
	app_injection_bean(MODEL_SERIAL,SerialService)
	app_injection_bean(MODEL_CMD,CmdService)
	app_injection_bean(MODEL_WIFI,WifiService)
	app_injection_bean(MODEL_TEST,TestService)
	app_injection_bean(MODEL_MQTT,MQTTService)
	app_injection_bean(MODEL_MINI_MP3,MiniMp3Service)
 	injectionField();
}
void IocService::injectionField() {
	for (auto &bean : _beans) {
		bean.second->____injection_field();
	}
}
void IocService::prepare0() {
	for (auto &bean : _beans) {
		bean.second->prepare0();
	}
}

void IocService::prepare() {
	for (auto &bean : _beans) {
		bean.second->prepare();
	}
}
void IocService::start() {
	for (auto &bean : _beans) {
		bean.second->start();
	}
}

void IocService::restart() {
	for (auto &bean : _beans) {
		bean.second->restart();
	}
}

void IocService::close() {
	int i = _end - 1;
	while (--i >= 0) {
		if (_beans.count(i) > 0) {
			_beans[i]->close();
		}
	}
}
void IocService::loop() {
	for (auto &bean : _beans) {
		bean.second->loop();
	}
}

IocService iocService;
